package com.team.party.controller;

import static com.team.party.util.PageConstant.LOGIN;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.team.party.bean.Activity;
import com.team.party.bean.Society;
import com.team.party.bean.Student;
import com.team.party.service.ManagerService;
import com.team.party.service.StudentService;
import com.team.party.service.impl.ManagerServiceImpl;
import com.team.party.service.impl.StudentServiceImpl;

@RestController
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private ManagerService managerservice;
    

    /**
     * @Description:进入登录页面
     * @param:
     * @return:String
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        return  LOGIN;
    }

    
    /**
     * @Description:用户登录
     */
    @RequestMapping("/login")
    public String login(){
    	return null;
    }

    /**
     * 通过社团号，查询管理的社团信息
     * @param: soc_no 社团号
     * @return:Society
     * @throws Exception 
     */
    @RequestMapping("/serchMySociety")
    public Society serchMySociety(
    		@RequestParam(value = "soc_no", required = false) String soc_no) throws Exception {
    	return managerservice.serchMySociety(soc_no);
    }
    
    
    /**
     * 通过社团号，修改社团信息
     * 允许修改：挂牌单位、所在校区、 负责人、负责人电话、当前人数、社团介绍
	 * @param soc_unit 单位
	 * @param soc_locat 校区
	 * @param soc_man 负责人
	 * @param soc_tele 负责人电话
	 * @param soc_count 当前人数
	 * @param soc_intro 社团介绍
	 * @param soc_no 社团号
     * @return:Boolean
     * @throws Exception 
     */
    @RequestMapping("/changeSocietyMessage")
    public Boolean changeSocietyMessage(
    		@RequestParam(value = "soc_unit", required = false) String soc_unit, 
    		@RequestParam(value = "soc_locat", required = false) String soc_locat, 
    		@RequestParam(value = "soc_man", required = false) String soc_man, 
    		@RequestParam(value = "soc_tele", required = false) String soc_tele, 
    		@RequestParam(value = "soc_count", required = false) int soc_count, 
    		@RequestParam(value = "soc_intro", required = false) String soc_intro, 
    		@RequestParam(value = "soc_no", required = false) String soc_no) throws Exception {
    	
    	return managerservice.changeSocietyMessage(soc_unit, soc_locat, soc_man, soc_tele, soc_count, soc_intro, soc_no);

    }
    
    
    /**
     * 查找本社团所有成员
	 * @param soc_no
     * @return:List<Student>
     * @throws Exception 
     */
    @RequestMapping("/serchSocietyNumber")
    public List<Student> serchSocietyNumber(
    		@RequestParam(value = "soc_no", required = false)  String soc_no) throws Exception{
    	
    	return managerservice.serchSocietyNumber(soc_no);
    }
    
    
    /**
     * 添加社团成员
     * @param stu_no 学生号
     * @param soc_no 社团号
     * @return:Boolean
     * @throws Exception 
     */
    @RequestMapping("/insertSocietyNumber")
    public Boolean insertSocietyNumber(
    		@RequestParam(value = "stu_no", required = false) String stu_no, 
    		@RequestParam(value = "soc_no", required = false) String soc_no,
    		HttpSession httpSession) throws Exception {
    	
    	Society society = (Society) httpSession.getAttribute("loginManager");
    	
    	society.setSoc_count(society.getSoc_count() + 1);
    	Boolean n =managerservice.insertSocietyNumber(stu_no, soc_no);
    	if(n) {
    		return  managerservice.changeSocietyMessage(society.getSoc_unit(), society.getSoc_locat(), society.getSoc_man(), society.getSoc_tele(), society.getSoc_count(), society.getSoc_intro(), society.getSoc_no());	
    	} else {
    		return false;
    	}
    }
    
    
    /**
     * 通过学生学号，修改社团成员信息
     * 允许修改：姓名、性别、年龄、电话、年级、学院
	 * @param stu_name 姓名
	 * @param stu_sex 性别
	 * @param stu_age 年龄
	 * @param stu_tel 电话
	 * @param stu_form 年级
	 * @param stu_scho 学院
	 * @param stu_no 学号
     * @return:Boolean
     * @throws Exception 
     */
    @RequestMapping("/changeNumberMessage")
    public Boolean changeNumberMessage(
    		@RequestParam(value = "stu_name", required = false) String stu_name, 
    		@RequestParam(value = "stu_sex", required = false) String stu_sex, 
    		@RequestParam(value = "stu_age", required = false) int stu_age, 
    		@RequestParam(value = "stu_tel", required = false) String stu_tel, 
    		@RequestParam(value = "stu_form", required = false) String stu_form, 
    		@RequestParam(value = "stu_scho", required = false) String stu_scho, 
    		@RequestParam(value = "stu_no", required = false) String stu_no) throws Exception {
//    	System.out.println("+++++++++++++++"+stu_scho);
    	return managerservice.changeNumberMessage(stu_name, stu_sex, stu_age, stu_tel, stu_form, stu_scho, stu_no);    
    }
    
    
    /**
     * 删除社团成员
     * @param stu_no 学生号
     * @param soc_no 社团号
     * @return:Boolean
     * @throws Exception 
     */
    @RequestMapping("/deleteSocietyNumber")
    public Boolean deleteSocietyNumber(
    		@RequestParam(value = "stu_no", required = false) String stu_no, 
    		@RequestParam(value = "soc_no", required = false) String soc_no,
    		HttpSession httpSession) throws Exception {
    	
    	Society society = (Society) httpSession.getAttribute("loginManager");
    	
    	society.setSoc_count(society.getSoc_count() - 1);
    	Boolean n = managerservice.deleteSocietyNumber(stu_no, soc_no);
    	if(n) {
    		return managerservice.changeSocietyMessage(society.getSoc_unit(), society.getSoc_locat(), society.getSoc_man(), society.getSoc_tele(), society.getSoc_count(), society.getSoc_intro(), society.getSoc_no());
    	} else {
    		return false;
    	}
    }
    
    
    
    /**
     * 查找本社团活动
     * @param soc_no 社团号
     * @return:List<Activity>
     * @throws Exception 
     */
    @RequestMapping("/serchMyActivity")
    public List<Activity> serchMyActivity(
    		@RequestParam(value = "soc_no", required = false) String soc_no) throws Exception{
    	
    	return managerservice.serchMyActivity(soc_no);
    }
    
    
    /**
     * 通过活动号查找活动
     * @param act_no 活动号 
     * @return:Activity
     * @throws Exception 
     */
    public Activity serchActivityByNo(
    		@RequestParam(value = "act_no", required = false) String act_no) throws Exception{
    	
    	return managerservice.serchActivityByNo(act_no);
    }
    
    /**
     * 发布活动
     * @param act_no 活动号
     * @param soc_no 社团号
     * @param act_name 活动名称
     * @param act_loc 活动地点
     * @param act_start 活动开始时间
     * @param act_end 活动结束时间
     * @param act_public 是否公开
     * @param act_intro 活动介绍
     * @param act_pic 活动图片
     * @throws Exception 
     */
    @RequestMapping("/saveActivity")
    public Boolean saveActivity(
    		@RequestParam(value = "act_no", required = false) String act_no, 
    		@RequestParam(value = "soc_no", required = false) String soc_no, 
    		@RequestParam(value = "act_name", required = false) String act_name, 
    		@RequestParam(value = "act_loc", required = false) String act_loc, 
    		@RequestParam(value = "act_start", required = false) String act_start, 
    		@RequestParam(value = "act_end", required = false) String act_end, 
    		@RequestParam(value = "act_public", required = false) int act_public, 
    		@RequestParam(value = "act_intro", required = false) String act_intro, 
    		@RequestParam(value = "act_pic", required = false) String act_pic) throws Exception {
    	System.out.println(act_start+"+++++++++++++++++++++++"+act_end);
    	
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date d_act_start = format.parse(act_start);
    	Date d_act_end = format.parse(act_end);
    	return managerservice.saveActivity(act_no, soc_no, act_name, act_loc, d_act_start, d_act_end, act_public, act_intro, act_pic);  
    }
    
    
    /**
     * 修改活动信息
     * 允许修改：活动名称、活动地点、开始时间、结束时间、活动介绍、活动图片
     * @param act_name 活动名称
     * @param act_loc 活动地点
     * @param act_start 活动开始时间
     * @param act_end 活动结束时间
     * @param act_intro 活动介绍
     * @param act_pic 活动图片
     * @param act_no 活动号
     * @throws Exception 
     */
    @RequestMapping("/changeActivityMessage")
    public Boolean changeActivityMessage(
    		@RequestParam(value = "act_name", required = false) String act_name, 
    		@RequestParam(value = "act_loc", required = false) 	String act_loc, 
    		@RequestParam(value = "act_start", required = false) String act_start, 
    		@RequestParam(value = "act_end", required = false)  String act_end, 
    		@RequestParam(value = "act_intro", required = false) String act_intro, 
    		@RequestParam(value = "act_pic", required = false) String act_pic, 
    		@RequestParam(value = "act_no", required = false) String act_no,
    		@RequestParam(value = "act_public", required = false) int act_public) throws Exception {
    	
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date d_act_start = format.parse(act_start);
    	Date d_act_end = format.parse(act_end);
    	return managerservice.changeActivityMessage(act_name, act_loc, d_act_start, d_act_end, act_intro, act_pic, act_no,act_public);		
    }
    
    
    /**
     * 删除活动
     * @param act_no 活动号
     * @return:Boolean
     * @throws Exception 
     */    
    @RequestMapping("/deleteActivity")
    public Boolean deleteActivity(
    		@RequestParam(value = "act_no", required = false) String act_no) throws Exception {
    	
    	return managerservice.deleteActivity(act_no);
    }
    
    
    
    
    /**
     * 通过学号、学生姓名模糊查询本社团成员
     * @param soc_no 社团号
     * @param stu_no 学生号
     * @param stu_name 学生姓名
     * @return List<Student>
     * @throws Exception
     */
    public List<Student> serchSocietyNumberByFuzzy(
    		@RequestParam(value = "soc_no", required = false) String soc_no, 
    		@RequestParam(value = "stu_no", required = false) String stu_no, 
    		@RequestParam(value = "stu_name", required = false)String stu_name) throws Exception{
    	
    	return managerservice.serchSocietyNumberByFuzzy(soc_no, stu_no, stu_name);
    }
    
    /**
     * 通过活动号、活动名模糊查询本社团活动
     * @param soc_no 社团号
     * @param act_no 活动号
     * @param act_name 活动名
     * @return List<Activity>
     * @throws Exception
     */
    @RequestMapping("/serchMyActivityByFuzzy")
    public List<Activity> serchMyActivityByFuzzy(
    		@RequestParam(value = "soc_no", required = false) String soc_no, 
    		@RequestParam(value = "act_no", required = false)String act_no, 
    		@RequestParam(value = "act_name", required = false)String act_name) throws Exception {
    	
    	return managerservice.serchMyActivityByFuzzy(soc_no, act_no, act_name);
    }
    
	/**
	 * 通过社团号查找社团人数
	 * @param soc_no 社团号
	 * @return int 社团人数
	 * @throws Exception
	 */
    @RequestMapping("/getSocietyNum")
    public int getSocietyNum(
    		@RequestParam(value = "soc_no", required = false)String soc_no) throws Exception {
    	return managerservice.getSocietyNum(soc_no);
    }
}
